<?php


namespace app\index\controller;

use app\common\model\User as CommonUserModel;
use \Faker;
use \app\common\controller\Base;
use think\facade\Request;
use think\Template;
use \think\facade\Validate;
use \app\index\model\User;
use think\Db;

class Test extends Base
{
    //测试用户的验证器
    public function test1()
    {
        $data = [
            'name' => 'xiaoniba ',
            'email' => 'admin@xiaoniba.com',
            'mobile' => '15823423319',
//            'password'=>'123abcd',
        ];

        $rule = 'app\common\validate\User';
        $res = $this->validate($data, $rule);
        var_dump($res);


    }

    function vl()
    {
        $validate = Validate::make(
            [
                'name' => 'require|max:25',
                'email' => 'email|require',
            ]
        );

        $data = Request::param();
        if ($validate->check($data)) {
            return '验证通过';

        } else {
            dump($validate->getError());
        }
    }

    function request()
    {
        dump('host' . '::' . Request::host());
        dump('scheme' . '::' . Request::scheme());
        dump('port' . '::' . Request::port());
        dump('remotePort' . '::' . Request::remotePort());
        dump('protocol' . '::' . Request::protocol());
        dump('contentType' . '::' . Request::contentType());
        dump('domain' . '::' . Request::domain());
        dump('subDomain' . '::' . Request::subDomain());
        dump('panDomain' . '::' . Request::panDomain());
        dump('rootDomain' . '::' . Request::rootDomain());
        dump('url' . '::' . Request::url());
        dump('baseUrl' . '::' . Request::baseUrl());
        dump('query' . '::' . Request::query());
        dump('baseFile' . '::' . Request::baseFile());
        dump('root' . '::' . Request::root());
        dump('rootUrl' . '::' . Request::rootUrl());
        dump('pathinfo' . '::' . Request::pathinfo());
        dump('path' . '::' . Request::path());
        dump('ext' . '::' . Request::ext());
        dump('time' . '::' . Request::time());
        dump('type' . '::' . Request::type());
        dump('method' . '::' . Request::method());
    }

    function rq(\think\Request $request)
    {

        dump($request->time());

    }

    function phpinfo()
    {
        phpinfo();


    }


    public function tt()
    {
        dump(User::get(92));
    }

    public function faker()
    {
        $faker = Faker\Factory::create('zh_CN');//选择中文
//        dump($faker);
        dump($faker->name());
        dump($faker->address());
        dump($faker->email());
        dump($faker->phoneNumber());
        dump(sha1($faker->password()));


    }

    public function random()
    {
        $num = $this->request->param('num');
        $faker = Faker\Factory::create('zh_CN');//选择中文

        for ($i = 0; $i < $num; $i++) {


            $name = $faker->name;
            $email = $faker->email;
            $mobile = $faker->phoneNumber;
            $password = sha1($faker->password);

            $data = [
                'name' => $name,
                'email' => $email,
                'mobile' => $mobile,
                'password' => $password,

            ];

            $rs = User::create($data, true);
//            echo $rs->id . '~';
            echo $rs->id . ' :: ' . $rs->name . '<br>';
        }
    }


    public function csdata($num)
    {
        $num = $this->request->param('num');
        $faker = Faker\Factory::create('zh_CN');//选择中文

        for ($i = 0; $i < $num; $i++) {


            $name = $faker->name;
            $email = $faker->email;
            $mobile = $faker->phoneNumber;
            $password = sha1($faker->password);

            $data = [
                'name' => $name,
                'email' => $email,
                'mobile' => $mobile,
                'password' => $password,

            ];

            $rs = User::create($data);
            dump($rs->id);
        }
    }


    public function getOne()
    {
        $rs = User::get(rand(1, 100000));
        dump($rs);

    }

    public function select()
    {
        $rs = User::field('*')->order('id', 'asc')->limit(100)->select();
        dump($rs);
    }

    public function getOneId($id)
    {

        $id ?: $id = Request::param('id');
        $rs = User::field('')->get($id);
        dump($rs);
    }

    public function updateOneId($id)
    {
        $id = Request::param('id');
        $row = User::get($id);


        Request::param('name') ? $row->name = Request::param('name') : null;
        Request::param('mobile') ? $row->mobile = Request::param('mobile') : null;
        strlen(Request::param('is_admin')) ? $row->is_admin = Request::param('is_admin') : null;
        strlen(Request::param('status')) ? $row->status = Request::param('status') : null;
        Request::param('password') ? $row->password = Request::param('password') : null;
        Request::param('email') ? $row->email = Request::param('email') : null;

        $rule = 'app\index\validate\User.update';//index/validate/User.update场景
        $valited = $this->validate($row, $rule);
//dump($res);exit;
        if (true === $valited) {
            $rs = $row->save();
            if ($rs) {
                echo '更新成功' . time();
            } else {
                $rs->getError();
            }
        } else {
            echo "数据检验错误: ";
            echo $valited;
        }


    }

    public function upCtime()
    {

        $data = User::whereNull('create_time')->select();
        foreach ($data as $v) {
            $up = User::where('id', $v['id'])->update(['create_time' => time()]);
            dump($up);
        }

    }

    public function upUtime()
    {
        $data = User::whereNull('update_time')->select();
        foreach ($data as $v) {
            $upd = User::where('id', $v['id'])->update(['update_time' => time()]);
            echo "Id为 $v\['id'\]更新成功";
        }
    }


    public function dbSelect()
    {
        $rows = Db::name('user')->select();
        dump($rows);

    }


    public function t1()
    {
        $rs = User::where('')->all([rand(1, 10), rand(1, 100), rand(1, 100)]);

        dump($rs);
    }


    public function loginCheck()
    {

            //验证数据
            $data = ['name' => 'test', 'password' => 'test'];
            //执行查询
            $rs = CommonUserModel::get(function ($query) use ($data) {
                $query->where('name', $data['name'])
                    ->where('password', sha1($data['password']));
            });

            dump($rs);

        }




}